package connectionPool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MyConnection {

	private Connection connection;
	private long lastUsageTime;
	private boolean inUse;
	private int connectionID;
	private static int counter = 0;

	public MyConnection(String url, String user, String passwd)
			throws SQLException {
		this.connection = DriverManager.getConnection(url, user, passwd);
		lastUsageTime = System.currentTimeMillis();
		connectionID = ++counter;
	}

	public void commit() throws SQLException {
		updateUsageTime();
		connection.commit();
	}

	public void rollback() throws SQLException {
		updateUsageTime();
		connection.rollback();
	}

	public void close() {
		lastUsageTime = 0;
	}

	void updateUsageTime() {
		lastUsageTime = System.currentTimeMillis();
	}

	protected long getLastUsageTime() {
		return lastUsageTime;
	}

	protected boolean isInUse() {
		return inUse;
	}

	protected void setInUse(boolean inUse) {
		this.inUse = inUse;
	}

	@Override
	public String toString() {
		return "Connection " + connectionID;
	}
}
